<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\common\controller\Login;
use think\Request;
use think\Db;
class Advertisement extends Login
{
	public function index()
	{


		$data = db('Advertisement')->order('sort desc')->paginate(5);

		$page = $data->render();

		$this->assign('page',$page);

		$this->assign('data',$data);

		return $this->fetch();

	}

	public function indexs($id)
	{
		$dat['fid'] = $id;

		$agent_id = session::get('agent_id');

		//$dat['agent_id'] = $agent_id;

		$data = db('Carousel')->where($dat)->order('sort desc')->paginate(5);

		$page = $data->render();

		$this->assign('page',$page);

		$this->assign('fid',$id);

		$this->assign('data',$data);

		return $this->fetch();
	}

	public function add($fid)
	{

		$this->assign('fid',$fid);

		return $this->fetch();
	}

	public function do_add()
	{
		$data = $_POST;
		//var_dump($data);die;
		 $file = request()->file('img');

				    $info = $file->validate(['ext'=>'jpg'])->move(ROOT_PATH . 'public/uploads/shop');
				    if($info){
				       $nowDate=date('Ymd');
				       $data['image'] = $nowDate.'/'.$info->getFilename();
				       $data['post_time'] = date('Y-m-d H:i:s');
				       $data['agent_id'] = session::get('agent_id');
				       $static = db('Carousel')->insert($data);

				       if ($static) {
				       		return $this->success('成功！');
				       }
				    }else{
				       return $this->error('错误!请重新上传图片.');
				    }

	}

	public function update($id)
	{
		$data = db('Carousel')->where('id',$id)->find();
		$this->assign('id',$id);
		$this->assign('data',$data);

		return $this->fetch();
	}

	public function do_update()
	{
		$data = $_POST;
		if (empty($data['sort']) || !is_numeric($data['sort'])) {
			return $this->error('排序应为整数！');
		}
		$file = request()->file('img');
		if($file){
			
			$info = $file->validate(['ext'=>'jpg'])->move(ROOT_PATH . 'public/uploads/shop');
			if($info){
				       $nowDate=date('Ymd');
				       $data['image'] = $nowDate.'/'.$info->getFilename();

				       
				    }else{
				       return $this->error('错误!请重新上传图片.');
				    }
		}
				    
					$static = db('Carousel')->update($data);

				       if ($static) {
				       	
				       		return $this->redirect('admin/Advertisement/indexs',array('id'=>$data['fid']));
				       }	    

	}

	public function delete($id)
	{
		$static = db('Carousel')->delete($id);

		if ($static) {
			return $this->success('成功！');
		}
	}

}
